package spring.framework.beans.factory.support;

import spring.framework.beans.BeansException;
import spring.framework.beans.factory.BeanFactory;
import spring.framework.beans.factory.config.BeanDefinition;

/**
 * @author fun.pengzh
 * @class spring.framework.beans.factory.support.AbstractBeanFactory
 * @desc
 * @since 2021-09-22
 */
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {


    @Override
    public Object getBean(String beanName) throws BeansException {
        return doGetBean(beanName, null);
    }

    @Override
    public Object getBean(String beanName, Object... args) throws BeansException {
        return doGetBean(beanName, args);
    }

    Object doGetBean(String beanName, Object... args)throws BeansException  {
        Object sinletonObject = getSingleton(beanName);
        if (null == sinletonObject) {
            BeanDefinition beanDefinition = getBeanDefinition(beanName);
            sinletonObject = createBean(beanDefinition, beanName, args);
        }
        return sinletonObject;
    }

    abstract BeanDefinition getBeanDefinition(String beanName);

    abstract Object createBean(BeanDefinition beanDefinition, String beanName, Object[] args)throws BeansException;

}
